 /* ****************************************

	计算核心代码来源于
	https://github.com/zinuzoid/zCalculator
	感谢作者zinuzoid

 *****************************************/

#include "calculator.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h> 

static lv_obj_t *ta;
static lv_obj_t *lab; 
static lv_obj_t *btnm1;

extern lv_obj_t *scr_tft;
extern lv_obj_t *scr_eink;
extern lv_obj_t *label_menu;

extern bool eink_scene_ready;
extern bool eink_flash_ready;


static void menu_close(void)
{
	lv_obj_del(ta);
	lv_obj_del(lab);
	lv_obj_del(btnm1);

	ta=NULL;
	lab=NULL;
	btnm1=NULL;
	lv_label_set_text(label_menu,"MENU");
	eink_scene_ready=false;
	eink_flash_ready=false;

}


static  void event_handler(lv_obj_t * obj, lv_event_t event)
{
	double realans,imagans;
	char postfix[50];
	char sbuf[50];

    if(event == LV_EVENT_VALUE_CHANGED) {
        const char * txt = lv_btnm_get_active_btn_text(obj);
        
		if(txt[0]=='=')
		{
			
			 
			Infix2Postfix( (char *)lv_ta_get_text(ta),postfix);
				
			Serial.println(postfix);
			
			EvalPostfix(postfix,&realans,&imagans); 

			lv_ta_set_text(ta,"");
			snprintf(sbuf,sizeof(sbuf),"%f",realans);
			// snprintf(sbuf,sizeof(sbuf),"%f %+f",realans,imagans);//复数形式
			lv_label_set_text(lab,sbuf);
			
            // lv_label_set_long_mode(lab,LV_LABEL_LONG_SROLL);
            lv_label_set_align(lab,LV_LABEL_ALIGN_RIGHT);
			lv_obj_align(lab,scr_tft,LV_ALIGN_IN_TOP_RIGHT,0,10); 
		}
        else if(txt[0]=='D')//del
        {
             
            lv_ta_del_char(ta);
        }
		else if(txt[0]=='<')//del
        {
            lv_ta_cursor_left(ta); 
        }
		else if(txt[0]=='>')//del
        {
            lv_ta_cursor_right(ta); 
        }
		else if(txt[0]=='C')//close
		{
			//关闭功能页面，进入主页
			menu_close();

		}
        else
        {
            lv_ta_add_text(ta,txt);
        }
        
        Serial.printf("%s was pressed\n", txt);
    }
}


static const char * btnm_map[] = {"CLOSE","<",">","\n",
								  "(", ")", "+", "-", "\n",
                                  "7", "8", "9", "/", "\n",
                                  "4", "5", "6", "*", "\n",
                                  "1", "2", "3", "Del", "\n",
                                  "0", ".", "=", ""};

static void lv_ex_btnm_1(void)
{
    btnm1 = lv_btnm_create(scr_eink, NULL);
    lv_btnm_set_map(btnm1, btnm_map);
    lv_btnm_set_btn_width(btnm1, 21, 2);        /*Make "Action1" twice as wide as "Action2"*/
	lv_btnm_set_btn_width(btnm1, 0, 2);        /*Make "Action1" twice as wide as "Action2"*/
    lv_obj_set_size(btnm1,176,264);
    lv_obj_align(btnm1, NULL, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_event_cb(btnm1, event_handler);
}





void calculator_create(lv_obj_t *obj, lv_event_t event)
{
    if(event == LV_EVENT_CLICKED) 
    {   
        // printf("Clicked\n");

        lv_label_set_text( label_menu,""); 

		// cont = lv_cont_create(scr_tft, NULL);
		// lv_obj_set_auto_realign(cont, true);                    /*Auto realign when the size changes*/
		// lv_obj_align_origo(cont, NULL, LV_ALIGN_CENTER, 0, 0);  /*This parametrs will be sued when realigned*/
		// lv_cont_set_fit(cont, LV_FIT_TIGHT);
		// lv_cont_set_layout(cont, LV_LAYOUT_COL_M);

        lab = lv_label_create(scr_tft, NULL);
		lv_obj_set_width(lab,160); 
		lv_label_set_text(lab,"calculator"); 
		lv_label_set_align(lab,LV_LABEL_ALIGN_CENTER);
		
        
        lv_obj_align(lab,scr_tft,LV_ALIGN_IN_TOP_MID,0,10);

        static lv_style_t style_lab;
        lv_style_copy(&style_lab,&lv_style_plain_color);
        style_lab.text.font = &lv_font_roboto_22;
        style_lab.text.color = LV_COLOR_WHITE;

        lv_label_set_style(lab,LV_LABEL_STYLE_MAIN,&style_lab);



        ta = lv_ta_create(scr_tft, NULL);
        lv_obj_set_size(ta,158,40);
        lv_ta_set_text(ta,"");
        lv_ta_set_one_line(ta,true);


        lv_obj_align(ta, scr_tft, LV_ALIGN_IN_BOTTOM_MID, 0, 0);

        static lv_style_t style_ta;
        lv_style_copy(&style_ta,&lv_style_plain_color);
        style_ta.text.font = &lv_font_roboto_22;

        lv_ta_set_style(ta,LV_TA_STYLE_BG,&style_ta); 
         
        
        lv_ex_btnm_1();
        eink_flash_ready = false;
        eink_scene_ready = false;

 


    }
    else if(event == LV_EVENT_VALUE_CHANGED) {
        printf("Toggled\n");
    }
}




